package com.lrs.java.jdbc.jdbc_teacher;
import java.sql.*;

public class TestMysqlConnection {

	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs?user=root&password=root");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from article");
			while(rs.next()) {
				System.out.println(rs.getInt(1)+ "-" + rs.getString("cont"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		} finally {
			try {
				if(rs != null) {
					rs.close();
					rs = null;
				}
				if(stmt != null) {
					stmt.close();
					stmt = null;
				}
				if(conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}
//create database bbs;
//use bbs;

//set names gbk;

//DROP TABLE IF EXISTS article;

//CREATE TABLE `article` (
//`id` int(11) default NULL,
//`cont` varchar(200) default NULL,
//`pid` int(11) default NULL,
//`isleaf` int(11) default NULL
//) ENGINE=MyISAM DEFAULT CHARSET=utf8;

//insert into article values (1, '蚂蚁大战大象', 0, 0);
//insert into article values (2, '大象被打趴下了', 1, 0);
//insert into article values (3, '蚂蚁也不好过', 2, 1);
//insert into article values (4, '瞎说', 2, 0);
//insert into article values (5, '没有瞎说', 4, 1);
//insert into article values (6, '怎么可能', 1, 0);
//insert into article values (7, '怎么没有可能', 6, 1);
//insert into article values (8, '可能性是很大的', 6, 1);
//insert into article values (9, '大象进医院了', 2, 0);
//insert into article values (10, '护士是蚂蚁', 9, 1);